package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@DubboService
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}", setmeal, checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        //for (Integer checkgroupId : checkgroupIds) {
        setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupIds);
        //}
        if (redisTemplate.hasKey(RedisConst.SETMEAL_LIST)) {
            redisTemplate.delete(RedisConst.SETMEAL_LIST);
        }
        if (redisTemplate.hasKey(RedisConst.SETMEAL+setmeal.getId())) {
            redisTemplate.delete(RedisConst.SETMEAL+setmeal.getId());
        }
        //redisTemplate.boundValueOps(RedisConst.SETMEAL_LIST).set(setMealDao.selectAll());
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public long countByImg(String img) {
        return setMealDao.countByImg(img);
    }

    @Override
    public List<Setmeal> findAll() {
        //调用DAO查询所有
        if (redisTemplate.hasKey(RedisConst.SETMEAL_LIST)) {
            List<Setmeal> list = (List) redisTemplate.boundValueOps(RedisConst.SETMEAL_LIST).get();
            return list;
        } else {
            List<Setmeal> setmeals = setMealDao.selectAll();
            redisTemplate.boundValueOps(RedisConst.SETMEAL_LIST).set(setmeals);
            return setmeals;
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        //调用DAO查询数据
        if (redisTemplate.hasKey(RedisConst.SETMEAL+id)){
            return (Setmeal)redisTemplate.boundValueOps(RedisConst.SETMEAL+id).get();
        }else {
            Setmeal setmeal = setMealDao.selectById(id);
            redisTemplate.boundValueOps(RedisConst.SETMEAL+id).set(setmeal);
            return setmeal;
        }
    }

    @Override
    public List<Integer> findCheckgroupIdsByCheckSetmealId(Integer id) {
        return setMealDao.selectCheckgroupIdsByCheckSetmealId(id);
    }
    @Transactional
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //1. 更新套餐
        setMealDao.update(setmeal);
        //2. 更新关联关系
        //删除原有关系
        setMealDao.deleteSetmealAndCheckgroupBySetmealId(setmeal.getId());
        //加入当前关联关系
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetmealAndCheckgroup(setmeal.getId(), checkgroupId);
        }
    }

    @Transactional
    @Override
    public void delete(Integer setmealId) {
        try {
            //1.删除关联关系
            setMealDao.deleteSetmealAndCheckgroupBySetmealId(setmealId);
            //2.删除套餐
            setMealDao.deleteSetmealById(setmealId);
        } catch (Exception e) {
            throw new BusinessRuntimeException("删除套餐失败");
        }
    }
}
